//
//  BIDSuggestionAndPraiseDetailViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/5/22.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDSuggestionAndPraiseDetailViewController.h"
#import "BIDSuggestionReplyCell.h"
#import "NSDate+Category.h"

static NSString *replyListURL = @"feedbackReplyAndroid/list.action";

@interface BIDSuggestionAndPraiseDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    BOOL _isRegister;
    BIDSuggestionReplyCell *_tempCell;
    NSArray *_dataSourceArr;
}
@end

@implementation BIDSuggestionAndPraiseDetailViewController
@synthesize suggestionID;
@synthesize suggestDate;
@synthesize suggestContent;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"建议与表扬";
    _tempCell = [[[NSBundle mainBundle] loadNibNamed:@"BIDSuggestionReplyCell" owner:self options:nil] lastObject];
    _dataSourceArr = [[NSArray alloc] init];
    _dateLabel.text = suggestDate;
    _contentLabel.text = suggestContent;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat height = [BIDGlobalMethods getHeightWithString:suggestContent font:_contentLabel.font constraintSize:CGSizeMake(screenSize.width-16*2, MAXFLOAT)];
    _heightForContentLabelConstraint.constant = height;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], replyListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"searchParam.feedbackId=%@&memberId=%@&page=1&rows=20", suggestionID, [AppDelegate getUserId]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            _dataSourceArr = [[NSArray alloc] initWithArray:responseDic[@"rows"]];
        }
        [_tableView reloadData];
    }];
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDSuggestionReplyCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDSuggestionReplyCell *replyCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = _dataSourceArr[row];
    replyCell.imgView.image = [UIImage imageNamed:AVATAR_PLACEHOLDER];
    replyCell.nameLabel.text = dic[@"replyUserName"];
    replyCell.contentLabel.text = dic[@"content"];
    NSDate *date = [BIDGlobalMethods transToNSDateByFormatter:@"yyyy-MM-dd HH-mm-ss" fromStringDate:dic[@"replyTime"]];
    replyCell.timeLabel.text = [date timeIntervalDescription];
    cell = replyCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    NSDictionary *dic = _dataSourceArr[indexPath.row];
    _tempCell.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(tableView.frame) - 69;
    _tempCell.contentLabel.text = dic[@"content"];
    CGSize size = [_tempCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    height = size.height < 89 ? size.height : size.height+1;
    return height;
}

@end
